import elementResizeDetectorMaker from 'element-resize-detector'
import type { MaybeRefOrGetter } from 'vue'

const erd = elementResizeDetectorMaker()

export default function useElementResize(
  resizeElem: MaybeRefOrGetter<HTMLElement>,
  callback: (elem: HTMLElement) => void,
) {
  erd.listenTo(toValue(resizeElem), callback)

  function removeAllListeners() {
    erd.removeAllListeners(toValue(resizeElem))
  }

  return removeAllListeners
}
